'use strict';


function watermark(settings) {
    //默认设置
    var defaultSettings = {
        watermark_txt: "text",
        watermark_x: 20,//水印起始位置x轴坐标
        watermark_y: 20,//水印起始位置Y轴坐标
        watermark_rows: 20,//水印行数
        watermark_cols: 20,//水印列数
        watermark_x_space: 100,//水印x轴间隔
        watermark_y_space: 50,//水印y轴间隔
        watermark_color: '#000000',//水印字体颜色
        watermark_alpha: 0.1,//水印透明度
        watermark_fontsize: '10px',//水印字体大小
        watermark_font: '微软雅黑',//水印字体
        watermark_width: 120,//水印宽度
        watermark_height: 80,//水印长度
        watermark_angle: 15//水印倾斜度数
    };
    var ds = defaultSettings;
    //采用配置项替换默认值，作用类似jquery.extend
    if (arguments.length === 1 && typeof arguments[0] === "object") {
        var src = arguments[0] || {};
        for (var key in src) {
            if (src[key] && ds[key] && src[key] === ds[key])
                continue;
            else if (src[key])
                ds[key] = src[key];
        }
    }
    //    var oTemp = document.createDocumentFragment();
    //获取页面最大宽度
    var page_width = Math.max(document.body.scrollWidth, document.body.clientWidth);
    //获取页面最大长度
    var page_height = Math.max(document.body.scrollHeight, document.body.clientHeight);
    if(page_height<650) {// 页面最小高度
        page_height = 650;
    }

    //如果将水印列数设置为0，或水印列数设置过大，超过页面最大宽度，则重新计算水印列数和水印x轴间隔
    if (ds.watermark_cols == 0 || (parseInt(ds.watermark_x + ds.watermark_width * ds.watermark_cols + ds.watermark_x_space * (ds.watermark_cols - 1)) > page_width)) {
        ds.watermark_cols = parseInt((page_width - ds.watermark_x + ds.watermark_x_space) / (ds.watermark_width + ds.watermark_x_space));
        ds.watermark_x_space = parseInt((page_width - ds.watermark_x - ds.watermark_width * ds.watermark_cols) / (ds.watermark_cols - 1));
    }
    //如果将水印行数设置为0，或水印行数设置过大，超过页面最大长度，则重新计算水印行数和水印y轴间隔
    if (ds.watermark_rows == 0 || (parseInt(ds.watermark_y + ds.watermark_height * ds.watermark_rows + ds.watermark_y_space * (ds.watermark_rows - 1))> page_height)) {
        ds.watermark_rows = parseInt((ds.watermark_y_space + page_height - ds.watermark_y) / (ds.watermark_height + ds.watermark_y_space));
        ds.watermark_y_space = parseInt((page_height - ds.watermark_y - ds.watermark_height * ds.watermark_rows) / (ds.watermark_rows - 1));
    }
    if(ds.watermark_rows<5) {
    // 针对行数过少进行处理
        ds.watermark_rows = 5;
    }

    const box = document.getElementById('watermark-box');
    while (box.firstChild) {
        box.removeChild (box.firstChild);
    }
    var child0 = box.childNodes;
    console.log("child0.length: " + child0.length + ', ds.watermark_rows:'+ds.watermark_rows+', ds.watermark_cols:'+ ds.watermark_cols);

    for (var i = 0; i < ds.watermark_rows; i++) {
        var y = ds.watermark_y + (ds.watermark_y_space + ds.watermark_height) * i;
        for (var j = 0; j < ds.watermark_cols; j++) {
            var x = ds.watermark_x + (ds.watermark_width + ds.watermark_x_space) * j;
            var mask_div = document.createElement('div');
            mask_div.id = 'mask_div' + i + j;
            mask_div.appendChild(document.createTextNode(ds.watermark_txt));
            //设置水印div倾斜显示
            mask_div.style.webkitTransform = "rotate(-" + ds.watermark_angle + "deg)";
            mask_div.style.MozTransform = "rotate(-" + ds.watermark_angle + "deg)";
            mask_div.style.msTransform = "rotate(-" + ds.watermark_angle + "deg)";
            mask_div.style.OTransform = "rotate(-" + ds.watermark_angle + "deg)";
            mask_div.style.transform = "rotate(-" + ds.watermark_angle + "deg)";
            mask_div.style.visibility = "";
            mask_div.style.position = "absolute";
            mask_div.style.left = x + 'px';
            mask_div.style.top = y + 'px';
            mask_div.style.overflow = "hidden";
            mask_div.style.zIndex = "9999";
            //mask_div.style.border="solid #eee 1px";
            mask_div.style.opacity = ds.watermark_alpha;
            mask_div.style.fontSize = ds.watermark_fontsize;
            mask_div.style.fontFamily = ds.watermark_font;
            mask_div.style.color = ds.watermark_color;
            mask_div.style.textAlign = "center";
            mask_div.style.width = ds.watermark_width + 'px';
            mask_div.style.height = ds.watermark_height + 'px';
            mask_div.style.display = "block";
            mask_div.style.pointerEvents = "none";
            mask_div.style.userSelect = "none";
            box.appendChild(mask_div);
        };
    };
    var child2 = box.childNodes;
    console.log("child2.length: " + child2.length + ', ds.watermark_rows:'+ds.watermark_rows+', ds.watermark_cols:'+ ds.watermark_cols);
}

window.addEventListener('resize', function() {
    if(typeof($GLOBAL_USER)!='undefined') {
        watermark({ watermark_txt: $GLOBAL_USER });
    }
});
window.addEventListener('scroll', function() {
    if(typeof($GLOBAL_USER)!='undefined') {
        watermark({ watermark_txt: $GLOBAL_USER });
    }
});